﻿using System;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace Dotnet.Utils.Utility.Zip
{
    public static class GZipUtil
    {
        public static byte[] Compress(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            byte[] result;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
                {
                    gzipStream.Write(bytes, 0, bytes.Length);
                    gzipStream.Close();
                    result = memoryStream.ToArray();
                }
            }
            return result;
        }

        public static string Decompress(byte[] bytes)
        {
            string @string;
            using (MemoryStream memoryStream = new MemoryStream(bytes))
            {
                using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (MemoryStream memoryStream2 = new MemoryStream())
                    {
                        byte[] buffer = new byte[1024];
                        int num = 0;
                        int num2;
                        while ((num2 = gzipStream.Read(buffer, 0, 1024)) > 0)
                        {
                            memoryStream2.Write(buffer, 0, num2);
                            num += num2;
                        }
                        @string = Encoding.UTF8.GetString(memoryStream2.GetBuffer(), 0, num);
                    }
                }
            }
            return @string;
        }

        public static byte[] DeflateCompress(byte[] data)
        {
            byte[] result;
            if (data == null || data.Length < 1)
            {
                result = data;
            }
            else
            {
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Compress))
                        {
                            deflateStream.Write(data, 0, data.Length);
                            deflateStream.Close();
                        }
                        result = memoryStream.ToArray();
                    }
                }
                catch (Exception)
                {
                    result = data;
                }
            }
            return result;
        }

        public static byte[] GzipCompress(byte[] data)
        {
            byte[] result;
            if (data == null || data.Length < 1)
            {
                result = data;
            }
            else
            {
                try
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
                        {
                            gzipStream.Write(data, 0, data.Length);
                            gzipStream.Close();
                        }
                        result = memoryStream.ToArray();
                    }
                }
                catch (Exception)
                {
                    result = data;
                }
            }
            return result;
        }
    }
}